package com.liming.salesman.webapp.wicket.page;


import com.liming.core.web.wicket.BasePage;
import com.liming.salesman.api.biz.IDistributorBeanService;
import com.liming.salesman.api.persistence.IDistributorBean;
import com.liming.salesman.api.persistence.ISalesmanBean;
import com.liming.salesman.entities.DistributorBeanImpl;
import com.liming.salesman.entities.SalesmanBeanImpl;
import org.apache.aries.blueprint.annotation.Reference;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormValidatingBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.util.time.Duration;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Salesman: sunlf
 * Date: 14-3-13
 * Time: 下午4:26
 * To change this template use File | Settings | File Templates.
 */
public class ListDistributor extends BasePage {

    @Named
    @Inject
    @Reference(id = "distributorBeanService", serviceInterface = IDistributorBeanService.class)
    private IDistributorBeanService distributorBeanService;

    Label label1= new Label("pageName1","添加销售顾问");
    Label label2= new Label("pageName2","编辑销售顾问");


    public ListDistributor() {

        label1.setVisible(true);
        label2.setVisible(false);
        add(label1);
        add(label2);

        final Form<IDistributorBean> form = new Form<IDistributorBean>("add_distributor_form", new CompoundPropertyModel<IDistributorBean>(new DistributorBeanImpl()));
        add(form);
        AjaxFormValidatingBehavior.addToAllFormComponents(form, "keydown", Duration.ONE_SECOND);

        TextField formName = new TextField("name");
        TextField formMobile = new TextField("mobile");
        TextField formAddress = new TextField("address");
        TextField formContact = new TextField("contact");
        TextField formZipcode = new TextField("zipcode");

        form.add(formName);
        form.add(formMobile);
        form.add(formAddress);
        form.add(formContact);
        form.add(formZipcode);

        form.add(new AjaxButton("submit_button", form) {
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> submitForm) {
                IDistributorBean iDistributorBean = (IDistributorBean) getParent().getDefaultModelObject();
                distributorBeanService.saveEntity(iDistributorBean);
                setResponsePage(ListDistributor.class);
                label1.setVisible(true);
                label2.setVisible(false);
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                System.out.println("onError");
            }
        });

        add(new Link("reset_from_1") {
            @Override
            public void onClick() {
                form.setModelObject(new DistributorBeanImpl());
                label1.setVisible(true);
                label2.setVisible(false);
            }
        });

        form.add(new Link("reset_from_2") {
            @Override
            public void onClick() {
                form.setModelObject(new DistributorBeanImpl());
                label1.setVisible(true);
                label2.setVisible(false);
            }
        });

        List<IDistributorBean> distributorList = distributorBeanService.getAllEntity();
        ListDataProvider<IDistributorBean> listDataProvider = new ListDataProvider<IDistributorBean>(distributorList);
        add(new DataView<IDistributorBean>("distributorRow", listDataProvider) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final Item<IDistributorBean> item) {
                final IDistributorBean distributor;
                distributor = item.getModelObject();

                item.add(new Label("name", distributor.getName()));
                item.add(new Label("mobile", distributor.getMobile()));
                item.add(new Label("address", distributor.getAddress()));
                item.add(new Label("contact", distributor.getContact()));
                item.add(new Label("zipcode", distributor.getZipcode()));
                item.add(new Link("edit") {
                    @Override
                    public void onClick() {
                        form.setModelObject((IDistributorBean) getParent().getDefaultModelObject());
                        label1.setVisible(false);
                        label2.setVisible(true);
                    }
                });
                item.add(new Link("del") {
                    @Override
                    public void onClick() {
                        add(new FeedbackPanel("feedback"));
                        distributorBeanService.deleteEntity(distributor.getId());
                        getSession().info("Is stored until OtherPage is rendered");
                        setResponsePage(ListDistributor.class);
                    }
                });

                item.add(AttributeModifier.replace("class", new AbstractReadOnlyModel<String>() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public String getObject() {
                        return (item.getIndex() % 2 == 1) ? "even" : "odd";
                    }
                }));
            }
        });
    }
}
